﻿using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace EZBarTender_Wrapper.EDLP
{
	public class DocumentPrinter : PrintDocument
	{
		private Font _font;
		private DataGridView _data;
		private int _leftMargin;
		private int _rightMargin;
		private int _topMargin;
		private int _bottomMargin;
		private int _width;
		private int _height;
		private int _headerHeight;
		private int _headerWidth;
		private int _footerHeight;
		private int _footerWidth;

		static int i = 0;
		static int pageNumber = 1;

		public DataGridView Data
		{
			get { return _data; }
			set { _data = value; }
		}

		public Font PrinterFont
		{
			get { return _font; }
			set { _font = value; }
		}

		public DocumentPrinter(PrinterSettings settings, DataGridView data)
			: base()
		{
			base.PrinterSettings = settings;
			base.DefaultPageSettings.Landscape = false;

			_data = data;
			_leftMargin = DefaultPageSettings.Margins.Left;// 25;
			_rightMargin = DefaultPageSettings.Margins.Right; // 25;
			_topMargin = DefaultPageSettings.Margins.Top; // 100;
			_bottomMargin = DefaultPageSettings.Margins.Bottom; // 100;
			_height = base.DefaultPageSettings.PaperSize.Height;
			_width = base.DefaultPageSettings.PaperSize.Width;
			_headerWidth = _width - _rightMargin - _leftMargin - 10;
			_headerHeight = _topMargin - 10;// 75;
			_footerWidth = _width - _rightMargin - _leftMargin - 10;
			_footerHeight = _bottomMargin - 10;// 75;
		}

		protected override void OnBeginPrint(PrintEventArgs e)
		{
			base.OnBeginPrint(e);

			if (_font == null)
				_font = Data.Font;
		}

		private void PrintHeader(
			Graphics g, string title, int pageNumber)
		{
			StringFormat rightAlign = new StringFormat();
			rightAlign.Alignment = StringAlignment.Far;
			g.DrawString(
				title, 
				PrinterFont,
				Brushes.Black,
				_leftMargin,
				_headerHeight - 20);

			g.DrawString(
				pageNumber.ToString(),
				PrinterFont,
				Brushes.Black,
				new RectangleF(_leftMargin + _headerWidth - 40, _headerHeight - 20, 40, 15),
				rightAlign);

			g.DrawLine(
				Pens.Black,
				_leftMargin,
				_headerHeight,
				_leftMargin + _headerWidth,
				_headerHeight);
		}

		private void PrintFooter(Graphics g, DateTime date, string program)
		{
			StringFormat rightAlign = new StringFormat();
			rightAlign.Alignment = StringAlignment.Far;

			g.DrawLine(
				Pens.Black, 
				_leftMargin, 
				_height - _footerHeight, 
				_footerWidth + _leftMargin, 
				_height - _footerHeight);
			g.DrawString(
				date.ToString("MM/dd/yyyy"),
				PrinterFont,
				Brushes.Black,
				_leftMargin,
				_height - _footerHeight + 5);
			g.DrawString(
				program,
				PrinterFont,
				Brushes.Black,
				new RectangleF(_width / 2, _height - _footerHeight + 5, _footerWidth / 2, 15),
				rightAlign);
		}

		protected override void OnPrintPage(PrintPageEventArgs e)
		{
			base.OnPrintPage(e);

			PrintHeader(e.Graphics, "EDLP Tear List", pageNumber);
			PrintFooter(e.Graphics, DateTime.Now, "EZBarTender");

			int penXLocation = _leftMargin;
			int penYLocation = _topMargin;

			StringFormat center = new StringFormat();
			center.Alignment = StringAlignment.Center;

			// Get the header line
			foreach (DataGridViewColumn column in Data.Columns)
			{
				int width = column.Width + 15;

				e.Graphics.DrawRectangle(Pens.Black, penXLocation, penYLocation,
					width, Data.Rows[0].Height);
				e.Graphics.FillRectangle(Brushes.DarkGray, penXLocation, penYLocation,
					width, Data.Rows[0].Height);
				e.Graphics.DrawString(
					column.HeaderText, 
					new Font(PrinterFont, FontStyle.Bold), 
					Brushes.Black,
					new RectangleF(penXLocation, penYLocation + 5, width,
						Data.Rows[0].Height), center);

				penXLocation += width;
			}

			// Print the rest of the data
			for (;  i < Data.Rows.Count; i++)
			{
				DataGridViewRow row = Data.Rows[i];

				penYLocation += Data.Rows[i].Height;
				penXLocation = _leftMargin;
				foreach (DataGridViewCell cell in row.Cells)
				{
					int width = cell.Size.Width + 15;

					e.Graphics.DrawRectangle(Pens.Black, penXLocation, penYLocation,
						width, row.Height);
					if (row.Index % 2 == 1)
						e.Graphics.FillRectangle(Brushes.LightGray, penXLocation,
							penYLocation, width, row.Height);

					if (cell.Value is DateTime)
					{
						e.Graphics.DrawString(((DateTime)cell.Value).ToString("MM/dd/yyyy"), PrinterFont,
							Brushes.Black, new RectangleF(penXLocation, penYLocation + 5,
								width, row.Height), center);
					}
					else if (cell.ColumnIndex == 0)
					{
						e.Graphics.DrawString(cell.Value.ToString(), PrinterFont,
							Brushes.Black, new RectangleF(penXLocation, penYLocation + 5,
								width, row.Height), center);
					}
					else
					{
						e.Graphics.DrawString(cell.Value.ToString(), PrinterFont,
							Brushes.Black, new RectangleF(penXLocation, penYLocation + 5,
								width, row.Height), StringFormat.GenericDefault);
					}
					penXLocation += width;
				}

				if (Data.Rows.Count > i)
				{
					if (penYLocation + Data.Rows[i].Height * 2 > _height - _bottomMargin)
					{
						penXLocation = _leftMargin;
						penYLocation = _topMargin;
						e.HasMorePages = true;
						i++;
						pageNumber++;
						return;
					}
				}
			}
		}
	}
}